package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApAuthor;
import com.heima.article.feign.ArticleFeign;
import com.heima.behavior.dto.FollowBehaviorDto;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.enums.UserEnum;
import com.heima.common.util.UserThreadLocalUtil;
import com.heima.user.dto.UserRelationDto;
import com.heima.user.entity.ApUserFollow;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.service.IApUserFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.assertj.core.internal.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP用户关注信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-08-12
 */
@Service
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow> implements IApUserFollowService {
    @Autowired
    private ArticleFeign articleFeign;
 @Autowired
  private KafkaTemplate<String ,String> kafkaTemplate;
 @Value("${topic.followBehaviorTopic}")
    private String followBehaviorTopic;

    /**
     * 关注作者
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(UserRelationDto dto) {
        //1.构建关注用户对象，属性赋值    有值才会到此 否则拦截器早就拦截住了 且 关注 取消关注代码都要so提到最上方
        User user = UserThreadLocalUtil.get(); //拦截器interceptor加WebConfig使拦截器生效，则拦截器把用户信息存入线程 ThreadLocal中了so可取出
        ApUserFollow apUserFollow = new ApUserFollow();//存 此 关注表so建此对象
        apUserFollow.setLevel(UserEnum.USER_FOLLOW_LEVEL_ONCE.getVal()); //关注度 0 偶尔感兴趣
        //1.1 设置登录用户信息
        apUserFollow.setUserId(user.getUserId());
        apUserFollow.setUserName(user.getUserName());
        //1.2 设置关注目标用户信息
        ResponseResult<ApAuthor> responseResult = articleFeign.getAuthorById(dto.getAuthorId());
        if (responseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            ApAuthor apAuthor = responseResult.getData(); //作者
            LambdaQueryWrapper<ApUserFollow> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApUserFollow::getUserId, user.getUserId());
            queryWrapper.eq(ApUserFollow::getFollowId, apAuthor.getUserId());
            if (dto.getOperation().equals(UserEnum.USER_FOLLOW_OPRATION.getVal())) {
                //关注操作
                apUserFollow.setFollowId(apAuthor.getUserId());
                apUserFollow.setFollowName(apAuthor.getName());

                //2.执行保存/删除   即关注就保存到ap_userfollow 关注表  取消关注则从此表删除   先查张三有无关注李四 拿张三和李四的id查，查到表关注过无需保存反之保存如张三 UserId 李四followId ，查到表有关注过无需保存了反之没关注过需要保存
                ApUserFollow one = this.getOne(queryWrapper);
                if (one == null) {
                    this.save(apUserFollow);
                }


            } else {
                //取消关注
                this.remove(queryWrapper);

            }
            //3. TODO 发送关注/取关行为消息
            FollowBehaviorDto followBehaviorDto = new FollowBehaviorDto();
               followBehaviorDto.setEquipmentId(dto.getEquipmentId());
            followBehaviorDto.setUserId(user.getUserId());
            followBehaviorDto.setFollowId(apAuthor.getUserId()); //作者用户的社交id
            followBehaviorDto.setOperation(dto.getOperation());
            kafkaTemplate.send(followBehaviorTopic, JSON.toJSONString(followBehaviorTopic)); // followBehaviorTopic是 heima-leadnews-user下的yml下的 topic 话题
        }                                            //要发的字符串消息转json
        return ResponseResult.okResult();
    }
}

